﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

/*
 * 脚本功能：学生端 开启UDP接收屏幕共享图片
 */
namespace ScreenTranslate
{
    public partial class Form1 : Form
    {
        private StudentNet student;
        private Size primiarySize;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            //pictureBox1.Size = new Size(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            student = new StudentNet(textBox1.Text, this);
            student.Start();
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (student != null)
            {
                student.Close();
            }
            Center.Instance.ShowMainPanel();
        }


        internal void SetImage(byte[] bytes)
        {
            Console.WriteLine("设置图片");
            pictureBox1.Image = BytesToImage(bytes);
            

            //label1.Text = Encoding.UTF8.GetString(bytes);
        }

        /// <summary>
        /// 数据转化成图片
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private Image BytesToImage(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Image _image = Image.FromStream(ms);
            return _image;
        }

        private void 设置屏幕最大化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            SetVisible(false);
            pictureBox1.Size = new Size(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
        }

        private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            SetVisible(true);
            pictureBox1.Size = new Size(757,507);
        }

        void SetVisible(bool bol)
        {
            label1.Visible = bol;
            textBox1.Visible = bol;
            button1.Visible = bol;
        }

        private void 全屏显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            Rectangle ret = Screen.GetWorkingArea(this);
            primiarySize = this.pictureBox1.ClientSize;
            this.pictureBox1.ClientSize = new Size(ret.Width,ret.Height);
            this.pictureBox1.Dock = DockStyle.Fill;
            this.pictureBox1.BringToFront();
        }

        private void 退出全屏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.WindowState = FormWindowState.Normal;
            this.pictureBox1.ClientSize = primiarySize;
            this.pictureBox1.Dock = DockStyle.None;
        }
    }
}
